package com.qg.lizhanqi.videosquare.uitlhelp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by zsh2722 on 2016/8/17.
 */
public class DiskCache {
    private Context mContext;
    private String cacheDir;
    public DiskCache(Context context) {
        mContext = context;
        // 获取缓存路径
        cacheDir=mContext.getCacheDir().getAbsolutePath()+"/";
    }

    // 获取图片
    public Bitmap get(String url) {
        return BitmapFactory.decodeFile(cacheDir+url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")) + ".png");
    }
    //讲图片存入本地
    public void put(String url,Bitmap bitmap) {
        FileOutputStream fileOutputStream=null;
        try {
            fileOutputStream=new FileOutputStream(cacheDir+url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")) + ".png");
            bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream!=null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
